package com.example.demo.servcie.impl;

import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import com.example.demo.model.ReaderReq;
import com.example.demo.model.ReaderResp;
import com.example.demo.servcie.IReaderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liunh
 */
@Slf4j
@Service
public class ReaderServiceImpl implements IReaderService, IExcelExportServer {

    @Override
    public List<Object> selectListForExcelExport(Object queryParams, int pageNumber) {
        ReaderReq req = (ReaderReq) queryParams;
        Page page = Page.of(pageNumber, 1000);
        PageResult<ReaderResp> dataList = this.page(req, page);
        return CollectionUtils.isEmpty(dataList) ? null : new ArrayList<>(dataList);
    }

    @Override
    public PageResult<ReaderResp> page(ReaderReq req, Page page) {
        int start = (page.getPageNumber() - 1) * page.getPageSize();
        int end = page.getPageNumber() * page.getPageSize();
        PageResult<ReaderResp> result = null;
        PageResult<ReaderResp> dataList = dataList(1000001);
        if (CollectionUtils.isNotEmpty(dataList) && start < dataList.size()) {
            result = new PageResult<>();
            for (int i = start; i < end; i++) {
                result.add(dataList.get(i));
            }
        }
        return result;
    }

    public static PageResult<ReaderResp> dataList(int count) {
        count = Math.max(count, 10001);
        PageResult<ReaderResp> dataList = new PageResult<>();
        for (int i = 1; i < count; i++) {
            dataList.add(new ReaderResp("ID" + i, "RFID" + i, "AccountNo" + i, "姓名" + i, "男", "2010-11-11"));
            dataList.add(new ReaderResp("ID" + i, "RFID" + i, "AccountNo" + i, "姓名" + i, "女", "2010-10-10"));
        }
        return dataList;
    }

}
